package com.kuang.controller;


import com.kuang.pojo.Books;
import com.kuang.pojo.Borrows;
import com.kuang.service.BorrowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.annotation.ModelAndViewResolver;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/borrow")
public class BorrowController {

    @Autowired
    @Qualifier("BorrowServiceImpl")
    private BorrowService borrowService;

    //查询全部记录
    @RequestMapping("/allBorrow")
    public String list(Model model){
        List<Borrows> list = borrowService.queryAllBorrow();
        model.addAttribute("list",list);
        return "borrows/allBorrow";
    }

    //增加一条记录
    @RequestMapping("/toAddBorrow")
    public String toAddPaper(){
        return "borrows/addBorrow";
    }

    //增加记录的请求
    @RequestMapping("/addBorrow")
    public String addBorrow(Borrows borrows){
        borrowService.addBorrow(borrows);
        return "redirect:/borrow/allBorrow";
    }

    //归还全部书籍（修改记录为0）
    @RequestMapping("/updateBorrow2/{borrowId}")
    public String updateBorrow(@PathVariable("borrowId") int id){
        borrowService.updateBorrowById(id);
        return "redirect:/borrow/allBorrow";
    }

//归还部分书籍（修改记录）
    @RequestMapping("/toUpdate")
    public String toUpdatePaper(int id,Model model){
        Borrows borrows=borrowService.queryBorrowById(id);
        model.addAttribute("Qborrow",borrows);
        return "borrows/updateBook";
    }

    //归还部分请求
    @RequestMapping("/updateBorrow")
    public String updateBorrow(Borrows borrows){
        borrowService.updateBorrow(borrows);
        return "redirect:/borrow/allBorrow";
    }

    //删除记录
    @RequestMapping("/deleteBorrow/{borrowId}")
    public String deleteBorrow(@PathVariable("borrowId") int id){
        borrowService.deleteBorrowById(id);
        return "redirect:/borrow/allBorrow";
    }

    //通过编号查询记录
    @RequestMapping("/querBorrow")
    public String querBorrow(String queryReadersNo,Model model){
        List<Borrows> borrows = borrowService.queryAllNo(queryReadersNo);
        List<Borrows> list = new ArrayList<Borrows>();
        list.addAll(borrows);
        if (borrows==null){
            list=borrowService.queryAllBorrow();
            model.addAttribute("error","未查到");
        }
        System.out.println(list);
        model.addAttribute("list",list);
        return "borrows/allBorrow";
    }

    @RequestMapping("/back")
    public String back(){
        return "index/index";
    }

}
